package com.xmtlxx.tms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.helper.LoginHelper;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.tms.domain.bo.ClientBo;
import com.xmtlxx.tms.domain.vo.CarrierVo;
import com.xmtlxx.tms.domain.vo.ClientVo;
import com.xmtlxx.tms.service.ICarrierService;
import com.xmtlxx.tms.service.IClientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 货主Controller
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Validated
@Api(value = "货主控制器", tags = {"货主管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/tms/client")
public class ClientController extends BaseController {

    private final IClientService iClientService;

    private final ICarrierService iCarrierService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<ClientVo> initBean(){
        ClientVo vo = new ClientVo();
        if(LoginHelper.getLoginUser().getRolePermission().contains("smscys")){
            vo.setIsShow(false);
            CarrierVo carrierVo = iCarrierService.selectCarrierByDeptId(LoginHelper.getDeptId());
            vo.setCarrierId(carrierVo.getId());
        }else {
            vo.setIsShow(true);
        }
        //可以对数据进行初始化
        return R.ok(vo);
    }

    /**
     * 查询货主列表
     */
    @ApiOperation("查询货主列表")
    @SaCheckPermission("tms:client:list")
    @GetMapping("/list")
    public TableDataInfo<ClientVo> list(@Validated(QueryGroup.class) ClientBo bo, PageQuery pageQuery) {
        return iClientService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出货主列表
     */
    @ApiOperation("导出货主列表")
    @SaCheckPermission("tms:client:export")
    @Log(title = "货主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated ClientBo bo, HttpServletResponse response) {
        List<ClientVo> list = iClientService.queryList(bo);
        ExcelUtil.exportExcel(list, "货主", ClientVo.class, response);
    }

    /**
     * 获取货主详细信息
     */
    @ApiOperation("获取货主详细信息")
    @SaCheckPermission("tms:client:query")
    @GetMapping("/{id}")
    public R<ClientVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") String id) {
        ClientVo vo = iClientService.queryById(id);
        if(LoginHelper.getLoginUser().getRolePermission().contains("smscys")){
            vo.setIsShow(false);
        }else {
            vo.setIsShow(true);
        }
        return R.ok(vo);
    }

    /**
     * 新增货主
     */
    @ApiOperation("新增货主")
    @SaCheckPermission("tms:client:add")
    @Log(title = "货主", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ClientVo vo) {
        ClientBo bo = new ClientBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iClientService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改货主
     */
    @ApiOperation("修改货主")
    @SaCheckPermission("tms:client:edit")
    @Log(title = "货主", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ClientVo vo) {
        ClientBo bo = new ClientBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iClientService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除货主
     */
    @ApiOperation("删除货主")
    @SaCheckPermission("tms:client:remove")
    @Log(title = "货主", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iClientService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
